import requests
import json
def create_kg(dataset_id,file_path,seg_tag):
    # 替换为你的实际参数

    api_key = 'dataset-Nx874tLa6L3Yc92jWRnpEw1G'



    # 构造请求 URL
    url = f"http://192.168.73.129:80/v1/datasets/{dataset_id}/document/create-by-file"

    # 构造 multipart/form-data 团队-业绩-数据
    data_payload = {
        "indexing_technique": "high_quality",
        "embedding_model":"bge-m3本地",
        "process_rule": {
            "rules": {
                "pre_processing_rules": [
                    {"id": "remove_extra_spaces", "enabled": True},
                    {"id": "remove_urls_emails", "enabled": True}
                ],
                "segmentation": {
                    "separator": seg_tag,
                    "max_tokens": 50000
                }
            },
            "mode": "custom"
        }
    }

    # 将 JSON 转换为字符串
    data_str = json.dumps(data_payload)

    # 设置请求头
    headers = {
        "Authorization": f"Bearer {api_key}"
    }

    # 发送 POST 请求
    try:
        with open(file_path, 'rb') as file:
            files = {
                # 注意：这里使用三元组格式 (文件名, 团队-业绩-数据, 内容类型)
                "data": (None, data_str, "text/plain"),
                "file": (file.name, file, "application/octet-stream")
            }
            print('files',files)
            response = requests.post(url, headers=headers, files=files)

        print(f"Status Code: {response.status_code}")
        print("Response:")
        print(response.json())

    except FileNotFoundError:
        print(f"Error: File {file_path} not found")
    except json.JSONDecodeError:
        print("Error: Failed to parse JSON response")
    except requests.RequestException as e:
        print(f"Request failed: {str(e)}")
dataset_id = '5292432a-524c-4296-b82a-708da3c39762'
file_path = 'E:/code_why/4sql自动生成/本地RAG-dawate-投标生成/out_json/user_questions.txt'
seg_tag="\n\n"
create_kg(dataset_id,file_path,seg_tag)